package gui.gui2d.highLevelPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import java.awt.event.MouseListener;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.text.DefaultCaret;

import core.ChatActionListenerSend;



public class ChatPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JScrollPane scrollPanelEdit;
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	ChatActionListenerSend cals = new ChatActionListenerSend(this);
	
	JButton send = new JButton("Invia");
	JTextArea textAreaEdit = new JTextArea("Inserisci il testo qui:");
	JTextArea textAreaLog= new JTextArea();
	public ChatPanel() {
		this.setOpaque(false);
		this.setLayout(new BorderLayout());
		p1.setOpaque(false);
		p2.setOpaque(false);
		this.add(p1,BorderLayout.CENTER);
		this.add(p2, BorderLayout.SOUTH);
		p1.setLayout(new FlowLayout());
		p2.setLayout(new FlowLayout());
		Border b = BorderFactory.createLineBorder(Color.WHITE);
		String carattere = "TimesRoman";
		int plain = Font.ITALIC;
		int dimensioni = 25;
		Font font = new Font(carattere, plain, dimensioni);
		TitledBorder tb=BorderFactory.createTitledBorder(b,"Chat",0,0,font);
		tb.setTitleColor(new Color(150,75,20));
		this.setBorder(tb);
		createGUIControl();
	}

	void createGUIControl(){
		textAreaLog.setOpaque(false);
		textAreaLog.setEditable(false);
		JScrollPane scrollPanelLog = new JScrollPane(textAreaLog, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPanelLog.setOpaque(false);
		scrollPanelLog.getViewport().setOpaque(false);
		scrollPanelLog.setPreferredSize(new Dimension(700,100));
		p1.add(scrollPanelLog);
		textAreaEdit.setOpaque(false);
		scrollPanelEdit= new JScrollPane(textAreaEdit);
		scrollPanelEdit.setOpaque(false);
		scrollPanelEdit.getViewport().setOpaque(false);
		scrollPanelEdit.setPreferredSize(new Dimension(600,50));
		p2.add(scrollPanelEdit);
		textAreaEdit.setLineWrap(true);
		textAreaEdit.setWrapStyleWord(true);
		textAreaLog.setLineWrap(true);
		textAreaLog.setWrapStyleWord(true);
		scrollPanelEdit.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPanelLog.setAutoscrolls(true);
		DefaultCaret caret = (DefaultCaret)textAreaLog.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		String carattere = "TimesRoman";
		int plain = Font.BOLD;
		int dimensioni = 17;
		Font font = new Font(carattere, plain, dimensioni);
		textAreaEdit.setFont(font);
		textAreaEdit.setForeground(Color.WHITE);
		p2.add(send);
		addListenerSendMessage(cals);
		textAreaLog.setFont(font);
		textAreaLog.setForeground(Color.WHITE);
		textAreaEdit.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent arg0) {
				JTextArea jta = (JTextArea) arg0.getComponent();
				if(jta.getText().equals("")){
					jta.setText("Inserisci il testo qui:");
				}
			}
			@Override
			public void focusGained(FocusEvent arg0) {
				JTextArea jta = (JTextArea) arg0.getComponent();
				if(jta.getText().equals("Inserisci il testo qui:")){
					jta.setText("");
				}
				
			}
		});

	
	}
	
	private String voidString(String nick){
		StringBuilder sb= new StringBuilder();
		for(int i=0;i<=nick.length();i++){
			sb.append("  ");
		}
		sb.append("");
		return sb.toString();
	}
	public void addMessage(String nick, String message){
		String replace= message.replaceAll("\n", "\n"+voidString(nick));
		textAreaLog.append(nick+": "+replace+"\n");

	}
	
	private void addListenerSendMessage(MouseListener ml){
		send.addMouseListener(ml);
	}
	
	public void addObserverSendButton(Observer o ){
		cals.addObserver(o);
	}
	public String getText(){
		return textAreaEdit.getText();
	}

	public void clearMessage(){
		textAreaEdit.setText("");
	}

}
